package com.ruoyi.web.controller.authentication;


import cn.hxzy.common.CertificationStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.authentication.domain.TsEnterpriseCertification;
import com.hxzy.authentication.service.TsEnterpriseCertificationService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-23
 */
@RestController
@RequestMapping("/tsEnterpriseCertification")
@DataSource(value = DataSourceType.AUTHENTICATION)
public class TsEnterpriseCertificationController {
    @Autowired
    private TsEnterpriseCertificationService tsEnterpriseCertificationService;

    @Value("${oss-img-path}")
    private String ossImgPath;

    /**
     * 列表显示
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/getEnterpriseList/{pageIndex}/{pageSize}")
    @Anonymous
    public AjaxResult getEnterpriseList(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<TsEnterpriseCertification> wrapper=new QueryWrapper<>();
        wrapper.notInSql("certification_status","101");
        List<TsEnterpriseCertification> list = tsEnterpriseCertificationService.list(wrapper);
        list.stream().forEach(item -> {
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return AjaxResult.success(pageInfo.getList()).put("total", total);
    }

    /**
     * 状态列表查询
     * @param pageIndex
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/getCertificationListByStatus/{pageIndex}/{pageSize}/{status}")
    public AjaxResult getCertificationListByStatus(@PathVariable Integer pageIndex, @PathVariable Integer pageSize, @PathVariable Integer status) {
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<TsEnterpriseCertification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certification_status", status);
        List<TsEnterpriseCertification> list = tsEnterpriseCertificationService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        list.stream().forEach(item -> {
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        return AjaxResult.success(pageInfo.getList()).put("total", total);
    }

    @Anonymous
    @PostMapping("/getQueryList/{pageIndex}/{pageSize}")
    public AjaxResult getQueryList(@RequestBody TsEnterpriseCertification tsEnterpriseCertification,@PathVariable Integer pageIndex,@PathVariable Integer pageSize){
       PageHelper.startPage(pageIndex,pageSize);
        List<TsEnterpriseCertification> queryList = tsEnterpriseCertificationService.getQueryList(tsEnterpriseCertification);
        PageInfo pageInfo=new PageInfo(queryList);
        long total = pageInfo.getTotal();
        queryList.stream().forEach(item->{
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        return AjaxResult.success(pageInfo.getList()).put("total",total);

    }



    @RequestMapping("/alluser")
    @Anonymous
    public AjaxResult getts() {
        List<TsEnterpriseCertification> tsEnterpriseCertification = tsEnterpriseCertificationService.getTsEnterpriseCertification();
        if (tsEnterpriseCertification != null) {
            return AjaxResult.success(tsEnterpriseCertification);
        }
        return AjaxResult.error("查询失败");
    }

    @Anonymous
    @RequestMapping("/{id}")
    public AjaxResult getxaingq(@PathVariable String id) {
        List<TsEnterpriseCertification> tsEnterpriseCertifications = tsEnterpriseCertificationService.getxqTsEnterpriseCertification(id);
        for (TsEnterpriseCertification tsEnterpriseCertification : tsEnterpriseCertifications){
            tsEnterpriseCertification.setIdCardFront(ossImgPath+tsEnterpriseCertification.getIdCardFront());
            tsEnterpriseCertification.setIdCardOpposite(ossImgPath+tsEnterpriseCertification.getIdCardOpposite());
            tsEnterpriseCertification.setBusinessLicense(ossImgPath+tsEnterpriseCertification.getBusinessLicense());
        }


        if (tsEnterpriseCertifications != null) {
            return AjaxResult.success(tsEnterpriseCertifications);
        }
        return AjaxResult.error("查询失败");
    }

    @Anonymous
    @GetMapping("/getimage/{id}")
    public AjaxResult getimage(@PathVariable String id) {
        TsEnterpriseCertification tsEnterpriseCertification = tsEnterpriseCertificationService.getimageTsEnterpriseCertification(id);

        return AjaxResult.success(tsEnterpriseCertification);
    }

    @Anonymous
    @PutMapping("/enterpriseAudit")
    public AjaxResult enterpriseAudit(@RequestBody TsEnterpriseCertification tsEnterpriseCertification) {

        tsEnterpriseCertificationService.updateById(tsEnterpriseCertification);

        return AjaxResult.success();
    }

}

